<?php

function version_str2ver($str) {
    return split(':', $str);
}

function version_ver2str($ver) {
    return $ver[0] . ':' . $ver[1];
}

function version_str2Vector($str) {
    $str = trim($str);
    if ($str == '') {
        $a = array();
    } else {
        $a = split(' ', $str);
        $a = array_map('version_str2ver', $a);
    }
    
    $vv = new version_Vector();
    foreach ($a as $v) {
	$vv->set($v[0], $v[1]);
    }
    return $vv;
}

function version_merge($va, $vb) {
    $vc = new version_Vector();
    $vc->merge($va);
    $vc->merge($vb);

    return $vc;
}

class version_Vector {
    function __construct($vers = array()) {
	$this->vers_ = $vers;
    }

    function __str() {
	return $this->to_str();
    }

    function to_str() {
	$a = array();
	foreach ($this->vers_ as $id => $v) {
	    array_push($a, $id . ':' . $v);
	}
	return implode(' ', $a);
    }

    function get($id) {
	if (array_key_exists($id, $this->vers_)) {
	    return $this->vers_[$id];
	}
	return 0;
    }

    function set($id, $ver) {
	$this->vers_[$id] = $ver;
    }

    function cmp($other) {
	$lg = false;
	$gt = false;

	foreach ($this->vers_ as $id => $ver) {
	    if ($ver < $other->get($id)) {
		$lt = true;
	    }
	    if ($ver > $other->get($id)) {
		$gt = true;
	    }
	}

	foreach ($other->vers_ as $id => $ver) {
	    if ($ver < $other->get($id)) {
		$gt = true;
	    }
	    if ($ver > $other->get($id)) {
		$lt = true;
	    }
	}

	if ($lt && $gt) {
	    return '|';
	} elseif ($lt) {
	    return '<';
	} elseif ($gt) {
	    return '>';
	} else {
	    return '=';
	}
    }

    function merge($other) {
	foreach ($other->vers_ as $id => $ver) {
	    if ($this->get($id) < $ver) {
		$this->set($id, $ver);
	    }
	}
    }
}

/*

$v1 = new version_Vector(array('a' => 3, 'b' => 4));
$v2 = new version_Vector(array('a' => 2, 'b' => 4));
$v3 = new version_Vector(array('a' => 5, 'b' => 1));
$v4 = new version_Vector(array('a' => 5, 'b' => 1));

assert($v1->cmp($v1) == '=');
assert($v2->cmp($v2) == '=');
assert($v3->cmp($v3) == '=');
assert($v1->cmp($v2) == '>');
assert($v2->cmp($v1) == '<');
assert($v1->cmp($v3) == '|');
assert($v3->cmp($v1) == '|');
assert($v2->cmp($v3) == '|');
assert($v3->cmp($v2) == '|');

*/

?>
